package cn.tedu.voice.system.controller;

import cn.tedu.voice.system.mvc.JsonPage;
import cn.tedu.voice.system.mvc.JsonResult;
import cn.tedu.voice.system.pojo.dto.VoiceAddNewDTO;
import cn.tedu.voice.system.pojo.dto.VoiceUpdateDTO;
import cn.tedu.voice.system.pojo.vo.VoiceListVO;
import cn.tedu.voice.system.service.IVoiceService;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/voices")
@Api(tags = "2.语音管理模块")
public class VoiceController {

    @Autowired
    private IVoiceService voiceService;

    @PostMapping("/add-new")
    @ApiOperation("上传语音")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@RequestBody @Valid VoiceAddNewDTO voiceAddNewDTO) {
        log.debug("开始处理上传语音");
        voiceService.addNew(voiceAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除语音")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> delete(@RequestBody Long[] ids) {
        log.debug("开始处理根据id删除语音");
        System.out.println(Arrays.toString(ids));
        voiceService.delete(ids);
        return JsonResult.ok();
    }

    @PostMapping("/{number:[0-9]+}/update")
    @ApiOperation("修改语音详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> update(@PathVariable("number") Integer voiceNumber, @RequestBody VoiceUpdateDTO voiceUpdateDTO) {
        log.debug("开始处理【修改语音详情】的业务，参数：{}", voiceNumber, voiceUpdateDTO);
        voiceService.update(voiceNumber, voiceUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("显示语音列表")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<VoiceListVO>> list() {
        List<VoiceListVO> list = voiceService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/page")
    @ApiOperation("分页查询语音包")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name="page",example ="1" ),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example ="10" )
    })
    public JsonResult<JsonPage<VoiceListVO>> page(Integer page, Integer pageSize){
        JsonPage<VoiceListVO> jsonPage= voiceService.list(page,pageSize);
        return JsonResult.ok(jsonPage);
    }

}
